// 引入node加密模块进行sha1加密
import crypto from 'crypto';
// env
import Env from '../env';
// config
import Config from  '../config';

const config = Config[Env];

export const wxValid = async ctx => {
  console.log(11);
  const { signature, timestamp, nonce, echostr } = ctx.query 
  const token = config.token
  let hash = crypto.createHash('sha1')
  const arr = [token, timestamp, nonce].sort()
  hash.update(arr.join(''))
  const shasum = hash.digest('hex')
  if(shasum === signature){
   return ctx.body = echostr
  }
  ctx.status = 401   
  ctx.body = 'Invalid signature'
};